package com.kedsoft.docms.controller;


import com.kedsoft.docms.common.MessageResult;
import com.kedsoft.docms.entity.Category;
import com.kedsoft.docms.entity.Content;
import com.kedsoft.docms.entity.Tag;
import com.kedsoft.docms.service.CategoryService;
import com.kedsoft.docms.service.ContentService;
import com.kedsoft.docms.service.TagService;
import com.kedsoft.docms.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LGP
 * @since 2018-11-29
 */
@Controller
@RequestMapping("/content")
public class ContentController {
    @Autowired
    private ContentService contentService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private TagService tagService;

    @RequestMapping(value = "index")
    public String index() {
        return "content/index";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public PageVo<Content> getPageList(Pagination<Content> pagination, @RequestParam(required = false) String title){
        Map<String, Object> query = new HashMap<>();
        query.put("title", title);
        contentService.pageList(pagination,query);
        PageVo<Content> pageVo = new PageVo<>(pagination.getItems(),pagination.getTotal());
        return pageVo;
    }

    @RequestMapping(value = "form")
    public String form(ModelMap map){
        //栏目列表
        List<CategorySelectVo> categoryList = categoryService.getListAll();
        //标签列表
        List<TagSelectVo> tagList = tagService.getAll();
        map.addAttribute("tagList",tagList);
        map.addAttribute("categoryList", categoryList);
        return "content/form";
    }

    @RequestMapping(value = "edit")
    public String edit(ModelMap map,@RequestParam int id){
        //栏目列表
        List<CategorySelectVo> categoryList = categoryService.getListAll();
        //标签列表
        List<TagSelectVo> tagList = tagService.getAll();
        Content bean = contentService.selectById(id);
        map.addAttribute("tagList",tagList);
        map.addAttribute("categoryList", categoryList);
        map.addAttribute("content",bean.getContent());
        return "content/edit";
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public MessageResult save(ContentVo param){
        Content bean = new Content();
        BeanUtils.copyProperties(param,bean);
        if(param.getCategoryid()!=0){
            Category p = categoryService.selectById(param.getCategoryid());
            bean.setCategoryname(p.getTitle());
        }
        if(param.getTagid()!=0){
            Tag t = tagService.selectById(param.getTagid());
            bean.setTagname(t.getTagname());
        }
        bean.setCrtime(new Date());
        contentService.insert(bean);
        return MessageResult.getSuccess("添加数据成功");
    }

    @RequestMapping(value = "update")
    @ResponseBody
    public MessageResult update(ContentVo param){
        Content bean = new Content();
        BeanUtils.copyProperties(param,bean);
        if(param.getCategoryid()!=0){
            Category p = categoryService.selectById(param.getCategoryid());
            bean.setCategoryname(p.getTitle());
        }
        if(param.getTagid()!=0){
            Tag t = tagService.selectById(param.getTagid());
            bean.setTagname(t.getTagname());
        }
        bean.setUptime(new Date());
        contentService.updateById(bean);
        return MessageResult.getSuccess("修改数据成功");
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public MessageResult delete(@RequestParam Integer ids){
        contentService.deleteById(ids);
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "remove")
    @ResponseBody
    public MessageResult deletes(@RequestParam String ids){
        String[] id = ids.split(",");
        for (String i: id) {
            Integer intId = Integer.parseInt(i);
            contentService.deleteById(intId);
        }
        return MessageResult.getSuccess("数据已删除");
    }
}

